export default {
    data() {
        return {
            region_id:0,
            ruleForm: {
                name: '',
                postData: {},
                sort: '',
                desc:'',
                radio:"1",
            },
            rules: {
                name: [
                    { required: true, message: '请输入大赛名称', trigger: 'blur' },
                ],
                desc: [
                    { required: true, message: '请输入大赛说明', trigger: 'blur' },
                ],
            },
        }
    },

    //进入页面加载
    mounted: function() {
        var that = this;
        //在缓存中获取值
        that.user_name = sessionStorage.getItem("user_name");
        that.user_id = sessionStorage.getItem("user_id");
        that.token = sessionStorage.getItem("access-token");
        if((!that.token) || (!that.user_id) || (!that.user_name)) {
            that.$router.push('/login');
        }
        //加载轮播详情
        if(that.$route.query.region_id) {
            that.region_id = that.$route.query.region_id;
            that.detail();
        }
    },

    //方法
    methods: {
        //请求api
        submitForm(formName) {
            let that = this;
            this.$refs[formName].validate((valid) => {
                if(valid) {
                    if(that.region_id === 0) {
                        that.add();
                    } else {
                        that.edit();
                    }
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },

        //添加
        add:function () {
            var that = this;
            //请求的数据
            var formData = {};
            formData.token = that.token;
            formData.user_id = that.user_id;
            formData.name = that.ruleForm.name;
            formData.sort = that.ruleForm.sort;
            formData.status = that.ruleForm.radio;
            formData.describe = that.ruleForm.desc;

            //请求api
            that.$http.post(that.adminApi.api_url + "/Region/add", formData, {
                emulateJSON: true
            }).then(
                function(res) {
                    // 处理成功的结果
                    that.$message({
                        type: 'success',
                        message: `操作提示: ${ '添加成功' }`
                    });
                    that.$router.push({path:'/megagame/region'});
                },
                function() {
                    // 处理失败的结果
                    that.$message({
                        type: 'error',
                        message: `操作提示: ${ '处理异常' }`
                    });
                });
        },

        /**
         * 详情
         */
        detail:function () {
            var that = this;
            //请求的数据
            var formData = {};
            formData.token = that.token;
            formData.id = that.region_id;

            //请求邀请者列表
            that.$http.post(that.adminApi.api_url + "/Region/show_edit", formData, {
                emulateJSON: true
            }).then(
                function(res) {
                    that.ruleForm.name=res.body.name;
                    that.ruleForm.sort=res.body.sort;
                    that.ruleForm.desc=res.body.describe;
                    that.ruleForm.radio=res.body.status.toString();
                },
                function() {
                    // 处理失败的结果
                    that.$message({
                        type: 'error',
                        message: `操作提示: ${ '处理异常' }`
                    });
                });
        },

        /**
         * 修改
         * @returns {boolean}
         */
        edit:function () {
            var that = this;
            //请求的数据
            var formData = {};
            formData.token = that.token;
            formData.user_id = that.user_id;
            formData.id = that.region_id;
            formData.name = that.ruleForm.name;
            formData.sort = that.ruleForm.sort;
            formData.status = that.ruleForm.radio;
            formData.describe = that.ruleForm.desc;

            //请求api
            that.$http.post(that.adminApi.api_url + "/Region/edit", formData, {
                emulateJSON: true
            }).then(
                function(res) {
                    // 处理成功的结果
                    that.$message({
                        type: 'success',
                        message: `操作提示: ${ '修改成功' }`
                    });
                    that.$router.push({path:'/megagame/region'});
                },
                function() {
                    // 处理失败的结果
                    that.$message({
                        type: 'error',
                        message: `操作提示: ${ '处理异常' }`
                    });
                });
        },

        /**
         * 返回
         */
        cancel:function () {
            let that = this;
            that.$router.go(-1)
        },
    }
}